<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div class="login-page">
    <el-card>
      <el-form :model="form" :rules="rules" ref="form">
        <img src="@/assets/logo.png" alt="" class="logo" />
        <el-form-item prop="loginName">
          <el-input
            placeholder="请输入账号"
            prefix-icon="el-icon-mobile-phone
"
            v-model="form.loginName"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            type="password"
            show-password
            v-model="form.password"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            placeholder="请输入验证码"
            prefix-icon="el-icon-circle-plus-outline"
            v-model="form.code"
            class="ipt"
          >
          </el-input>
          <el-button class="btn1"
          @click="clickfn1"
            ><img :src="yanzhen" alt="" style="width: 100%"
          /></el-button>
        </el-form-item>
        <el-form-item>
          <el-button class="btn" @click="clickfn">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { login, Code } from '@/api/user'
export default {
  data () {
    return {
      yanzhen: '',
      form: {
        loginName: 'admin',
        password: 'admin',
        code: ''

        // code: ''
      },
      clientToken: Math.floor(Math.random() * 10000) + 1,
      rules: {
        loginName: [
          {
            required: true,
            message: '请输入账号',
            trigger: ['blur', 'change']
          }

        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: ['blur', 'change']
          }

        ],
        code: [
          { required: true, message: '请输入验证码', trigger: ['blur', 'change'] }

        ]
      }
    }
  },
  methods: {
    async clickfn () {
      // 第一个参数表示是否校验成功，第二个参数表示未通过的字段
      // this.$refs.ze.validate((a, b) => {
      //   console.log(a, b)
      // })
      // validate方法不传函数就返回一个promise实例
      try {
        const valid = await this.$refs.form.validate()
        // console.log(valid)
        if (valid) {
          const res = await login({
            loginName: this.form.loginName,
            password: this.form.password,
            code: this.form.code,
            clientToken: this.clientToken,
            loginType: 0

          })
          console.log(res)

          // this.$store.commit('user/setUserToken', data.token)
          this.$router.push('/')
        }
      } catch (e) {
        console.error(e)
      }
    },
    async clickfn1 () {
      const res = await Code(this.form.clientToken)
      console.log(res)
      this.yanzhen = `data:image/jpeg;base64,${btoa(
      new Uint8Array(res).reduce(
        (data, byte) => data + String.fromCharCode(byte),
        ''
      )
    )}`
      // console.log(res1)
    }
  },
  async created () {
    const res = await Code()
    console.log(res)
    this.yanzhen = `data:image/jpeg;base64,${btoa(
      new Uint8Array(res).reduce(
        (data, byte) => data + String.fromCharCode(byte),
        ''
      )
    )}`
    console.log(this.yanzhen)
  }
}
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  background: url(@/assets/下载.png) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .el-card {
    position: absolute;
    width: 518px;
    height: 338px;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 76px 35px 0;
    background: #fff;
    overflow: initial !important;
  }

  .logo {
    position: absolute;
    width: 96px;
    height: 96px;
    top: -46px;
    left: 50%;
    margin-left: -48px;
    margin-bottom: 20px;
  }
  .tc {
    text-align: center;
  }
  .btn {
    width: 448px;
    height: 52px;
    color: #fff;
    background: linear-gradient(262deg, #2e50e1, #6878f0);
  }
  .ipt {
    width: 346px;
    height: 40px;
  }
  .btn1 {
    padding: 0;
    transform: translateY(40%);
    border: none;
  }
  ::v-deep .el-input__inner {
    height: 50px;
  }
}
</style>
